home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Games of Daze
/
Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso
/
x2ftp
/
msdos
/
gamesrc
/
rtanksrc
/
interp.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-07
|
20KB
|
730 lines
/**--------------------------------------------------------------
**
**--------------------------------------------------------------
** MODULE : interp.c
** PURPOSE : <t> Interprete compiler output
** PROGRAMMER : Sandy
** START DATE : 11/29/1988 11:55:11
** DESCRIPTION:
** :
** :
**==============================================================
**/
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include <math.h>
#include <dtypes.h>
#include <string.h>
#include "shop.h"
#include "interp.h"
#include "graph.h"
#include "bullet.h"
#include "tconfig.h"
#define BASERANGE 50
#define MAX_X 75
#define MAX_Y 43
BYTE ps1[] = {
0x02, 0x04, /* Sizex, Sizey */
0x03, 0xc0, 0x0b, 0xe0, 0x23, 0xc8, 0x23, 0xc8,
0x20, 0x08, 0x20, 0x08, 0x0a, 0xa0, 0x00, 0x00
};
BYTE ps2[] = {
0x02, 0x04, /* Sizex, Sizey */
0x00, 0x03, 0x0a, 0xBC, 0x20, 0xF8, 0x23, 0xE8,
0x20, 0x08, 0x20, 0x08, 0x0a, 0xa0, 0x00, 0x00
};
BYTE ps3[] = {
0x02, 0x04, /* Sizex, Sizey */
0x00, 0x00, 0x0a, 0xa0, 0x20, 0x08, 0x20, 0xFF,
0x20, 0xFF, 0x20, 0x08, 0x0a, 0xa0, 0x00, 0x00
};
BYTE ps4[] = {
0x02, 0x04, /* Sizex, Sizey */
0x00, 0x00, 0x0a, 0xa0, 0x20, 0x08, 0x20, 0x08,
0x23, 0xE8, 0x20, 0xF8, 0x0a, 0xBC, 0x00, 0x03
};
BYTE ps5[] = {
0x02, 0x04, /* Sizex, Sizey */
0x00, 0x00, 0x0a, 0xa0, 0x20, 0x08, 0x20, 0x08,
0x23, 0xc8, 0x23, 0xc8, 0x0b, 0xe0, 0x03, 0xc0
};
BYTE ps6[] = {
0x02, 0x04, /* Sizex, Sizey */
0x00, 0x00, 0x0a, 0xa0, 0x20, 0x08, 0x20, 0x08,
0x23, 0xC8, 0x2F, 0x08, 0x3C, 0xa0, 0xF0, 0x00
};
BYTE ps7[] = {
0x02, 0x04, /* Sizex, Sizey */
0x00, 0x00, 0x0a, 0xa0, 0x20, 0x08, 0xFF, 0x08,
0xFF, 0x08, 0x20, 0x08, 0x0a, 0xa0, 0x00, 0x00
};
BYTE ps8[] = {
0x02, 0x04, /* Sizex, Sizey */
0xF0, 0x00, 0x3C, 0xa0, 0x2F, 0x08, 0x23, 0xC8,
0x20, 0x08, 0x20, 0x08, 0x0a, 0xa0, 0x00, 0x00
};
BYTE noblk[] = {
0x02, 0x04, /* Sizex, Sizey */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
BYTE ps0[] = {
0x02, 0x04, /* Sizex, Sizey */
0x00, 0x00, 0x0a, 0xa0, 0x20, 0x08, 0x20, 0x08,
0x20, 0x08, 0x20, 0x08, 0x0a, 0xa0, 0x00, 0x00
};
char far *blank;
extern void CALLASM(int x, int y, char far *buf);
extern void SETMODE(void);
BOT players[10];
int debug;
int nplayers;
BOOL b_goodread;
BYTE field[MAX_X][MAX_Y];
#include "code.h"
/*<f>----------------------------------------
* FUNCTION: <s> void v_do_arena(PSTR s[])
* PURPOSE : Mass combat function
* :
* CREATION: 12/05/1988 14:17:44
*/
void v_do_arena(PSTR s[])
{
FILE *fp;
int a;
char temp[50];
char config[50];
randomize();
v_init_playfield();
for (a=0; a<10; a++) {
strcpy(temp,s[a+1]);
strcpy(config,s[a+1]);
strcat(temp,".BIN");
strcat(config,".DAT");
if ((*s[a+1]!=' ') && ((fp=fopen(temp,"rb"))!=NULL)) {
fclose(fp);
if ((fp=fopen(config,"rb"))!=NULL) {
fclose(fp);
v_init_bot(&players[a]);
b_goodread=FALSE;
v_load_file(temp, &players[a]);
v_read_tank_from_disk(config,&players[a].tank_config);
if (!b_goodread)
players[a].active=FALSE;
else
players[a].active=TRUE;
} else players[a].active=FALSE;
} else players[a].active=FALSE;
}
blank=noblk; debug=0;
v_init_bullets();
SETMODE();
v_draw_screen("ARENA BATTLE");
printf("%u",coreleft());
for (a=0; a<10; a++)
v_draw_player(&players[a]);
while (!kbhit()) {
for (a=0; a<10; a++)
if (players[a].active)
v_interprete(&players[a]);
v_move_bullets();
}
a=getkey();
for (a=0; a<10; a++)
v_free_interpreter_ram(&players[a]);
textmode(2);
} /* void v_do_arena(PSTR s[]) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_interp_main(PSTR s)
* PURPOSE : Program start
* :
* CREATION: 11/29/1988 11:56:18
*/
void v_interp_main(PSTR s)
{
int a;
debug=1;
v_init_playfield();
v_init_bot(&players[0]);
b_goodread=FALSE;
v_load_file(s, &players[0]);
if (!b_goodread)
return;
blank=noblk;
SETMODE();
v_draw_screen(s);
v_draw_player(&players[0]);
v_init_bullets();
while (!kbhit()) {
v_interprete(&players[0]);
v_move_bullets();
}
a=getkey();
v_free_interpreter_ram(&players[0]);
textmode(2);
} /* void v_interp_main(PSTR s) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_load_file(PSTR s, p_BOT bot)
* PURPOSE : Load in a compiler binary file
* :
* CREATION: 11/29/1988 11:58:11
*/
void v_load_file(PSTR s, p_BOT bot)
{
int a;
FILE *fp;
if ((fp=fopen(s,"rb"))==NULL) {
b_goodread=FALSE;
return;
}
bot->fsize=getw(fp);
for (a=0; a<bot->fsize; a++) {
if (bot->nlines>MAXLINES) {
printf("Too many lines for interpreter\n");
exit(1);
}
if ((bot->line[bot->nlines] = (p_ZLINE) malloc(sizeof(ZLINE)))==NULL) {
printf("Cannot malloc ram for tank code\n");
exit(1);
}
bot->line[bot->nlines]->n_command =getw(fp);
bot->line[bot->nlines]->pline =getw(fp);
bot->line[bot->nlines]->arg1.n_ArgType=getw(fp);
bot->line[bot->nlines]->arg1.n_value =getw(fp);
bot->line[bot->nlines]->arg2.n_ArgType=getw(fp);
bot->line[bot->nlines]->arg2.n_value =getw(fp);
bot->line[bot->nlines]->arg3.n_ArgType=getw(fp);
bot->line[bot->nlines]->arg3.n_value =getw(fp);
bot->nlines++;
}
fclose(fp);
b_goodread=TRUE;
} /* void v_load_file(PSTR s, p_BOT bot) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_init_bot(p_BOT pbot)
* PURPOSE : Initilize a bot
* :
* CREATION: 11/29/1988 12:24:40
*/
void v_init_bot(p_BOT pbot)
{
int a;
pbot->bax=1+(rand() % MAX_X);
pbot->bay=1+(rand() % MAX_Y);
pbot->bx=1+pbot->bax;
pbot->by=(pbot->bay<<1)+6;
memset(pbot->vars,0,sizeof(int)*100);
for (a=0; a<MAXLINES; a++)
pbot->line[a]=NULL;
pbot->cdir=pbot->gdir=1;
pbot->nlines=0;
pbot->cline=0;
pbot->ready=FALSE;
pbot->depth=0;
pbot->pic[1]=ps1;
pbot->pic[2]=ps2;
pbot->pic[3]=ps3;
pbot->pic[4]=ps4;
pbot->pic[5]=ps5;
pbot->pic[6]=ps6;
pbot->pic[7]=ps7;
pbot->pic[8]=ps8;
} /* void v_init_bot(p_BOT pbot) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_interprete(p_BOT p)
* PURPOSE : Interprete compiler code
* :
* CREATION: 11/29/1988 12:34:43
*/
void v_interprete(p_BOT p)
{
p_ZLINE cl;
BOOL next;
if (debug==0)
delay(5);
if (p->cline>p->nlines) {
printf("Gone beyond program end @%d of %d\n",p->cline,p->nlines);
exit(1);
}
cl=p->line[p->cline];
next=TRUE;
switch (cl->n_command) {
case _WHEREX : v_store(p, &cl->arg1,p->bax);
break;
case _WHEREY : v_store(p, &cl->arg1,p->bay);
break;
case _RADAR : if (p->tank_config.extrasType[RADAR]>0)
v_store(p, &cl->arg1, n_check_radar(p) );
break;
case _SCAN : v_store(p, &cl->arg1,
n_check_scan(p,n_get_arg(p,&cl->arg2)));
break;
case _CGDIR : v_store(p, &cl->arg1,p->gdir);
break;
case _CTDIR : v_store(p, &cl->arg1,p->cdir);
break;
case _LEFT : p->cdir--;
if (p->cdir==0) p->cdir=8;
break;
case _RIGHT : p->cdir++;
if (p->cdir==9) p->cdir=1;
break;
case _GLEFT : p->gdir--;
if (p->gdir==0) p->gdir=8;
v_redraw_player(p);
break;
case _GRIGHT : p->gdir++;
if (p->gdir==9) p->gdir=1;
v_redraw_player(p);
break;
case _LOADED : v_store(p, &cl->arg1,p->ready);
break;
case _MOVE : p->obx=p->bx; p->oby=p->by;
p->obax=p->bax; p->obay = p->bay;
v_ido_move(p);
v_erase_player(p);
v_draw_player(p);
break;
case _FIRE : v_shoot(p);
break;
case _JUMP : p->cline=n_get_arg(p,&cl->arg1);
next=FALSE;
break;
case _CALL : v_do_call(p,&cl->arg1);
next=FALSE;
break;
case _RETURN : v_do_return(p);
break;
case _JUMPLESS: if (n_get_arg(p,&cl->arg1) < n_get_arg(p,&cl->arg2)) {
p->cline=n_get_arg(p,&cl->arg3);
next=FALSE;
}
break;
case _JUMPEQ : if (n_get_arg(p,&cl->arg1) == n_get_arg(p,&cl->arg2)) {
p->cline=n_get_arg(p,&cl->arg3);
next=FALSE;
}
break;
case _JUMPNEQ : if (n_get_arg(p,&cl->arg1) != n_get_arg(p,&cl->arg2)) {
p->cline=n_get_arg(p,&cl->arg3);
next=FALSE;
}
break;
case _JUMPGRT : if (n_get_arg(p,&cl->arg1) > n_get_arg(p,&cl->arg2)) {
p->cline=n_get_arg(p,&cl->arg3);
next=FALSE;
}
break;
case _SET : v_store(p,&cl->arg1,n_get_arg(p,&cl->arg2));
break;
case _ADD : v_store(p,&cl->arg1,
n_get_arg(p,&cl->arg1)+n_get_arg(p,&cl->arg2)
);
break;
case _RAND : v_store(p,&cl->arg1,1+(rand() % n_get_arg(p,&cl->arg2)));
break;
}
if (next) p->cline++;
} /* void v_interprete(p_BOT p) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_store(p_BOT pb, p_ZARG pa, int n)
* PURPOSE : Store constant N in argument PA
* :
* CREATION: 11/29/1988 12:41:59
*/
void v_store(p_BOT pb, p_ZARG pa, int n)
{
if (pa->n_ArgType!=SYMBOL) {
printf("Assignment destination must be a symbol, line %d\n",pb->line[pb->cline]->pline);
exit(1);
}
pb->vars[pa->n_value]=n;
} /* void v_store(p_BOT pb,p_ZARG pa, int n) */
/*<f>----------------------------------------
* FUNCTION: <s> int n_get_arg(p_BOT pbot, p_ZARG p)
* PURPOSE : Return the value of an argument
* :
* CREATION: 11/29/1988 12:56:51
*/
int n_get_arg(p_BOT pbot, p_ZARG p)
{
if (p->n_ArgType==SYMBOL)
return (pbot->vars[p->n_value]);
else if (p->n_ArgType==CONSTANT)
return p->n_value;
else return p->n_value;
} /* int n_get_arg(p_BOT pbot, p_ZARG p) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_ido_move(p_BOT p)
* PURPOSE : Move in current direction
* :
* CREATION: 11/29/1988 13:01:43
*/
void v_ido_move(p_BOT p)
{
int hx,hy, hx1, hy1;
hx =p->bx; hy =p->by;
hx1=p->bax; hy1=p->bay;
switch (p->cdir) {
case 1 : if (p->bay>1) {p->bay--; p->by-=2; }
break;
case 2 : if (p->bax<MAX_X) {p->bax++; p->bx++; }
if (p->bay>1) {p->bay--; p->by-=2; }
break;
case 3 : if (p->bax<MAX_X) {p->bax++; p->bx++; }
break;
case 4 : if (p->bax<MAX_X) {p->bax++; p->bx++; }
if (p->bay<MAX_Y) {p->bay++; p->by+=2; }
break;
case 5 : if (p->bay<MAX_Y) {p->bay++; p->by+=2; }
break;
case 6 : if (p->bay<MAX_Y) {p->bay++; p->by+=2; }
if (p->bax> 1) {p->bax--; p->bx--; }
break;
case 7 : if (p->bax> 1) {p->bax--; p->bx--; }
break;
case 8 : if (p->bax> 1) {p->bax--; p->bx--; }
if (p->bay> 1) {p->bay--; p->by-=2; }
break;
}
if (field[p->bx][p->by]>=100) {
p->bx=hx; p->by =hy;
p->bax=hx1; p->bay=hy1;
}
} /* void v_ido_move(p_BOT p) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_do_call(p_BOT pb, p_ZARG pa)
* PURPOSE : Call subroutine at PA
* :
* CREATION: 11/29/1988 14:21:52
*/
void v_do_call(p_BOT pb, p_ZARG pa)
{
if (pb->depth>19) {
printf("Too many GOSUB's\n");
exit(1);
}
pb->gosub[pb->depth++]=pb->cline;
pb->cline=n_get_arg(pb,pa);
} /* void v_do_call(p_BOT pb, p_ZARG pa) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_do_return(p_BOT pb)
* PURPOSE : Do a return
* :
* CREATION: 11/29/1988 14:23:35
*/
void v_do_return(p_BOT pb)
{
if (pb->depth<1) {
printf("RETURN without GOSUB\n");
exit(1);
}
pb->cline=pb->gosub[--pb->depth];
} /* void v_do_return(p_BOT pb) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_free_interpreter_ram(p_BOT p)
* PURPOSE : Free up ram allocated by interpreter
* :
* CREATION: 12/05/1988 16:27:40
*/
void v_free_interpreter_ram(p_BOT p)
{
int a;
for (a=0; a<MAXLINES; a++)
if (p->line[a]!=NULL)
free(p->line[a]);
} /* void v_free_interpreter_ram(p_BOT p) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_shoot(p_BOT p)
* PURPOSE : Fire a bullet in current direction
* :
* CREATION: 12/05/1988 19:16:09
*/
void v_shoot(p_BOT p)
{
switch (p->gdir) {
case 1 : v_init_bullet(p->bx,p->by,0,-2,BASERANGE);
break;
case 2 : v_init_bullet(p->bx+1,p->by,1,-2,BASERANGE);
break;
case 3 : v_init_bullet(p->bx+1,p->by,1,0,BASERANGE);
break;
case 4 : v_init_bullet(p->bx+1,p->by+3,1,2,BASERANGE);
break;
case 5 : v_init_bullet(p->bx,p->by+3,0,2,BASERANGE);
break;
case 6 : v_init_bullet(p->bx,p->by+3,-1,2,BASERANGE);
break;
case 7 : v_init_bullet(p->bx,p->by,-1,0,BASERANGE);
break;
case 8 : v_init_bullet(p->bx,p->by,-1,-2,BASERANGE);
break;
}
} /* void v_shoot(p_BOT p) */
/*<f>----------------------------------------
* FUNCTION: <s> BOOL b_check_for_hit(int x, int y)
* PURPOSE : Check if point (x,y) hits a tank
* :
* CREATION: 12/06/1988 09:21:46
*/
BOOL b_check_for_hit(int x, int y)
{
int a;
a=1;
if (a==1) return FALSE; /* temp */
if (field[x][y]==0) return FALSE;
for (a=0; a<10; a++)
if (players[a].active)
if ((players[a].bx==x || players[a].bx+1==x) &&
(players[a].by==y || players[a].by+1==y || players[a].by+2==y || players[a].by+3==y)
) return a+1;
return FALSE;
} /* BOOL b_check_for_hit(int x, int y) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_flash_tank(int a)
* PURPOSE : Flash a tank after being hit
* :
* CREATION: 12/06/1988 09:28:33
*/
void v_flash_tank(int a)
{
int b;
if (!players[a].active) {
printf("HIT ERROR");
exit(1);
}
for (b=0; b<5; b++) {
v_draw_player(&players[a]);
delay(50);
v_erase_player(&players[a]);
delay(20);
}
} /* void v_flash_tank(int a) */
/*<f>----------------------------------------
* FUNCTION: <s> int n_check_radar(p_BOT p)
* PURPOSE : Look around for close tanks
* : and return distance
* CREATION: 12/06/1988 11:49:48
*/
int n_check_radar(p_BOT p)
{
int f,d,a;
int x,y;
for (f=999, a=0; a<10; a++)
if (players[a].active && p!=&players[a]) {
x=abs(players[a].bax-p->bax);
y=abs(players[a].bay-p->bay);
x=x*x; y=y*y;
d=(int) sqrt((double)(x+y));
if (d<f) f=d;
}
return ((f==999) ? 0 : f);
} /* int n_check_radar(p_BOT p) */
/*<f>----------------------------------------
* FUNCTION: <s> int n_check_scan(p_BOT p, int n)
* PURPOSE : Perform a scan for robot p in direction n
* :
* CREATION: 12/06/1988 14:02:03
*/
int n_check_scan(p_BOT p, int n)
{
int dx,dy,a,adx, ady;
for (a=0; a<10; a++)
if (players[a].active && p!=&players[a]) {
dx=p->bax-players[a].bax;
dy=p->bay-players[a].bay;
adx=abs(dx);
ady=abs(dy);
switch (n) {
case 1 : if ((dx==0) && (dy> 0)) return TRUE;
break;
case 2 : if ((dx< 0) && (dy> 0) && (adx==ady)) return TRUE;
break;
case 3 : if ((dx<0 ) && (dy==0)) return TRUE;
break;
case 4 : if ((dx< 0) && (dy< 0) && (adx==ady)) return TRUE;
break;
case 5 : if ((dx==0) && (dy< 0)) return TRUE;
break;
case 6 : if ((dx> 0) && (dy< 0) && (adx==ady)) return TRUE;
break;
case 7 : if ((dx> 0) && (dy==0)) return TRUE;
break;
case 8 : if ((dx> 0) && (dy> 0) && (adx==ady)) return TRUE;
break;
}
}
return FALSE;
} /* int n_check_scan(p_BOT p, int n) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_init_playfield(void)
* PURPOSE : Create playfield
* :
* CREATION: 12/06/1988 14:44:58
*/
void v_init_playfield(void)
{
memset(field,0,sizeof(field));
} /* void v_init_playfield(void) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_draw_player(p_BOT p)
* PURPOSE : Show player on screen
* :
* CREATION: 12/06/1988 14:46:17
*/
void v_draw_player(p_BOT p)
{
if (p->active) {
CALLASM(p->bx,p->by,p->pic[p->gdir]);
field[p->bax][p->bay]=1;
}
} /* void v_draw_player(p_BOT p) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_redraw_player(p_BOT p)
* PURPOSE : Redraw player on screen (Do not update field)
* :
* CREATION: 12/06/1988 15:17:53
*/
void v_redraw_player(p_BOT p)
{
if (p->active)
CALLASM(p->bx,p->by,p->pic[p->gdir]);
} /* void v_redraw_player(p_BOT p) */
/*<f>----------------------------------------
* FUNCTION: <s> void v_erase_player(p_BOT p)
* PURPOSE : Erase player on screen
* :
* CREATION: 12/06/1988 14:49:03
*/
void v_erase_player(p_BOT p)
{
if (p->active) {
CALLASM(p->obx,p->oby,blank);
field[p->obax][p->obay]=0;
}
} /* void v_erase_player(p_BOT p) */